package com.chat.demo.userservice.config;

import org.springframework.context.event.EventListener;
import org.springframework.messaging.simp.user.SimpUserRegistry;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.messaging.SessionConnectEvent;


@Component
public class WebSocketEventLogger {

    private final SimpUserRegistry simpUserRegistry;

    public WebSocketEventLogger(SimpUserRegistry simpUserRegistry) {
        this.simpUserRegistry = simpUserRegistry;
    }

    @EventListener
    public void handleConnect(SessionConnectEvent event) {
        System.out.println("🔌 WebSocket 已连接，用户: " +
                (event.getUser() != null ? event.getUser().getName() : "匿名"));

        // 打印所有在线用户
        simpUserRegistry.getUsers().forEach(user -> {
            System.out.println("在线用户：" + user.getName());
        });
    }
}